package com.baway.biliclitent.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.model.TagsBean;
import com.baway.biliclitent.util.OkHttpUtils;
import com.baway.biliclitent.util.UrlUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * 发现 Fragment
 */

public class FragmentDiscover extends Fragment implements View.OnClickListener {

    private CardView mCardView;
    private TagFlowLayout mTags;
    private TagFlowLayout mHideTags;
    private TextView mMore;
    private List<TagsBean.ListBean> list;
    private TextView mText;
    private NestedScrollView mScrollView;
    private boolean IsShow = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover,container,false);
        Log.d("zhulei","111111111111111");
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initHeader();
        initWidget();

    }

    @Override
    public void onResume() {
        super.onResume();
        getUrl();
    }

    private void getUrl() {

        OkHttpUtils.getHttpUtils().loadDataFromNet(UrlUtils.HOT_SEARCH,
                TagsBean.class, new OkHttpUtils.CallBackListener<TagsBean>() {

            @Override
            public void onSuccess(TagsBean result) {
                String s = result.toString();
                list = result.getList();
                Log.d("zhulei","Tag" + list.get(0).getKeyword());
                setWidgetStart();
            }

            @Override
            public void onFail() {

            }
        });
    }

    private void initHeader() {

    }

    private void initWidget() {
        mCardView = (CardView) getActivity().findViewById(R.id.card_view);
        mTags = (TagFlowLayout) getActivity().findViewById(R.id.tags_layout);
        mHideTags = (TagFlowLayout) getActivity().findViewById(R.id.hide_tags_layout);
        mMore = (TextView) getActivity().findViewById(R.id.tv_more);
        mScrollView = (NestedScrollView) getActivity().findViewById(R.id.gone_scrollview);
    }

    private void setWidgetStart() {
        initTagLayout();
        mMore.setOnClickListener(this);
    }

    private void initTagLayout() {

        List<TagsBean.ListBean> listBeen = list.subList(0, 8);
        Log.d("zhulei","Tag" + listBeen.get(0).getKeyword());
        mTags.setAdapter(new TagAdapter(listBeen) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {

                mText = (TextView) LayoutInflater.from(
                        getActivity()).inflate(R.layout.layout_tags_item, parent, false);
                mText.setText(list.get(position).getKeyword());
                return mText;
            }
        });

        mHideTags.setAdapter(new TagAdapter(list) {
            @Override
            public View getView(FlowLayout parent, int position, Object o) {

                mText = (TextView) LayoutInflater.from(
                        getActivity()).inflate(R.layout.layout_tags_item, parent, false);
                mText.setText(list.get(position).getKeyword());
                return mText;
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.tv_more :
                YesOnNo();
                break;
        }
    }

    private void YesOnNo() {

        if (IsShow){

            IsShow = false;
            mTags.setVisibility(View.GONE);
            mMore.setText("收起");
            mScrollView.setVisibility(View.VISIBLE);

        }else {

            IsShow = true;
            mTags.setVisibility(View.VISIBLE);
            mMore.setText("查看更多");
            mScrollView.setVisibility(View.GONE);

        }
    }
}
